import axios from "axios";
import {errorMessage, rejectMessage, successMessage} from "@/assets/js/utils";


// 请求拦截器，内部根据返回值，重新组装，统一管理。
axios.interceptors.response.use(res => {
    if (typeof res !== 'object') {

        console.log('服务端异常！');
        return Promise.reject(res)
    }


    // 响应正常
    if (res.status === 200) {
        let data=res.data
        if(data.code === 401){
            errorMessage("登录失效,请重新登录")
            localStorage.removeItem("token")

        }
    // 响应不正常
    }else if(res.status === 401 ){
        if (res) {
            alert(res.data)
        }
        return Promise.reject(res)
    }

    return res
})


class Service {
    // 获取所有文章
    getAllArticles() {
        return axios.get("/api/article").then(resp=>resp.data)
    }

// 获取所有标签
    getTabs() {
        return axios.get("/api/tabs").then(resp=>resp.data)
    }

    getTop10Articles(){
        return axios.get("/api/article/top10").then(resp=>resp.data)
    }

    getAllResource(pageNum){
        if(pageNum === undefined){
            pageNum=1
        }
        return axios.get("/api/res/all?pageNum="+pageNum).then(resp=>resp.data)
    }

    getCodeArticle(){
        return axios.get("/api/article/id?id=1").then(resp=>resp.data)
    }


    deleteRes(id){
        return axios.get("/api/ylb/res/delete?id="+id,{
            headers:{
                token:localStorage.getItem("token")
            }
        }).then(resp=>resp.data)
    }


    search(search){

      return  axios.get("/api/article/search?info="+search).then(resp=>resp.data)

    }

    addView(id){
        axios.get("/api/article/view?id="+id).then(resp=>resp.data)
    }
    deleteLog(id){
        return axios.delete("/api/ylb/log?id="+id,{
            headers:{
                token:localStorage.getItem("token")
            }
        })
    }

    getAllStaticFiles(pageNum){

        if(pageNum === undefined){
            pageNum=1;
        }


        return axios.get("/api/file/all?pageNum="+pageNum).then(resp=>resp.data,reason => {
            const {status, statusText} = reason.response
            alert("错误: "+status+"\n原因: "+statusText)


        })
    }

    deleteStaticFiles(url){
        return axios.delete("/api/ylb/file/rm",{
            params:{
                fileUrls:url.toString()
            },
            headers:{
                token:localStorage.getItem("token")|| ''
            }
        }).then(resp=>resp.data)
    }
    getAllMusics(){
        return axios.get("/api/music/all").then(resp=>resp.data)
    }

    insertMusic(music){
        return axios.post("/api/music/insert",{
           ...music
        }).then(resp=>resp.data)
    }
    // key:'day'、'week'、'month'、'year'  bool: true（has ips） 、false(not ips，just has length)
    getVisitor(key,bool){
        return axios.get("/api/webInfo/visitor/"+key+"?bool="+bool,{
            headers:{
                token:localStorage.getItem("token")
            }
        }).then(resp=>resp.data)
    }

    addVisitor(){
        axios.get("http://www.youlibin.top:8090/webInfo/visitor").then(r => r.data)
    }
    getAllPictures(){
        return axios.get("/api/picture/all").then(r => r.data)
    }
    getAddr(){
        return axios.get("http://www.youlibin.top:8090/webInfo/getAddr").then(resp=>resp.data)
    }
    getAddrInfo(ip,id){
        id=id?id:1
        return axios.get("/api/webInfo/getAddr/info?ip="+ip+"&id="+id).then(resp=>resp.data)
    }

    get(url){
        return axios.get(url).then(resp=>resp.data)
    }


    checkChooseClass(cookie){
        return axios.get("/api/cc/check?cookie="+cookie).then(resp=>resp.data)
    }
    afterChooseClass(cookie,classId){
        return axios.get("/api/cc?cookie="+cookie+"&classId="+classId).then(resp=>resp.data)
    }


}
var service=new Service()
export  default  service